package com.eva.ss.model;

import com.eva.framework.rbac.model.RbacUserInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@ApiModel("Spring Security用户信息")
public class SsUserInfo extends RbacUserInfo implements UserDetails {

    /**
     * 获取授权集合
     * 注意：Spring Security中并不会直接区分角色和权限，统一视为授权，所以角色编码和权限编码不能一致
     */
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> authorities = new HashSet<>();
        // 添加角色
        if (this.getRoles() != null) {
            for (String role : this.getRoles()) {
                authorities.add(new SimpleGrantedAuthority(role));
            }
        }
        // 添加权限
        if (this.getPermissions() != null) {
            for (String permission : this.getPermissions()) {
                authorities.add(new SimpleGrantedAuthority(permission));
            }
        }
        return authorities;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
